home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / RAVE Starter Samples / Texture Sample / Texture Triangle.cp < prev    next >
Encoding:
Text File  |  1998-04-30  |  6.1 KB  |  205 lines  |  [TEXT/CWIE]

  1. /*********************************************************************************
  2.  
  3. Texture Triangles.cp
  4. Author: Timothy Carroll
  5. Apple Developer Technical Support
  6. devsupport@apple.com
  7.  
  8.  
  9. Much like the Gouraud shaded triangles sample, this one demonstrates
  10. texture mapping.
  11.  
  12.  
  13. Modification History: 
  14.  
  15. 5/1/98        TMC     Initial Release
  16.  
  17.  
  18. Copyright © 1998 Apple Computer, Inc., All Rights Reserved
  19.  
  20.  
  21. You may incorporate this sample code into your applications without
  22. restriction, though the sample code has been provided "AS IS" and the
  23. responsibility for its operation is 100% yours.  However, what you are
  24. not permitted to do is to redistribute the source as "DSC Sample Code"
  25. after having made changes. If you're going to re-distribute the source,
  26. we require that you make it clear in the source that the code was
  27. descended from Apple Sample Code, but that you've made changes.
  28. **********************************************************************************/
  29.  
  30. #include <RAVE.h>
  31. #include <QD3DAcceleration.h>  // We need this for engine gestalt codes
  32. #include "Common Stuff.h"
  33. #include "RAVE Utilities.h"
  34.  
  35.  
  36. void main (void);
  37. void DoRAVEWindow(void);
  38.  
  39.  
  40. void main (void)
  41. {
  42.     // do standard mac init
  43.     InitGraf(&qd.thePort);
  44.     InitFonts();
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs(nil);
  49.     InitCursor();
  50.  
  51.     MaxApplZone();
  52.     MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters(); MoreMasters();
  53.  
  54.     // Initialize the SIN and COS lookup tables (see RAVE Utilities.cp)
  55.     if (InitializeLookups() == noErr)
  56.         DoRAVEWindow();
  57. }
  58.  
  59. void DoRAVEWindow(void)
  60. {
  61.     // These hold our errors
  62.     OSErr            theErr = noErr;
  63.     TQAError        theQAErr = kQANoErr;
  64.     
  65.     // These hold the window and device we'll put on the screen
  66.     GDHandle        device = NULL;
  67.     WindowRef        theWindow = NULL;
  68.     
  69.     // We try to center the window on the device.
  70.     Rect            bounds = {0,0,320,320};
  71.     Rect            deviceRect;
  72.     
  73.     // We use these parameters to describe our environment to RAVE
  74.     TQADevice        qaDevice;
  75.     TQARect         qaBoundsRect;
  76.     
  77.     // we get these items back from RAVE
  78.     TQAEngine         *theEngine = NULL;
  79.     TQADrawContext    *theContext = NULL;
  80.     
  81.     // We use these in our drawing loop to create our RAVE animation.
  82.     int             loop = 0;
  83.     TQATexture         *texture = NULL;
  84.     TQAVTexture     theTriangle[3];
  85.     
  86.     RgnHandle        gray;
  87.     Rect            grayRect;
  88.     
  89.     
  90.     // First, grab the first monitor with the deepest display
  91.     gray = GetGrayRgn();
  92.     grayRect = (**gray).rgnBBox;
  93.     device = GetMaxDevice(&grayRect);
  94.     FAIL_NIL (device, "\pERROR: No monitors found")
  95.     
  96.     
  97.     // Center our bounds in the middle of the screen.
  98.     deviceRect = (**device).gdRect;
  99.  
  100.     OffsetRect (&bounds,
  101.                     (deviceRect.right+deviceRect.left-bounds.right)/2 ,
  102.                     (deviceRect.bottom+deviceRect.top-bounds.bottom)/2);
  103.     
  104.     // Create a Window to hold the RAVE information.
  105.         
  106.     theWindow = NewCWindow(NULL, &bounds, "\pRAVE Window", true, zoomDocProc, (WindowRef) -1, false, 0);
  107.     FAIL_NIL (theWindow, "\pERROR: Couldn't create RAVE Window")
  108.     
  109.     SetPortWindowPort (theWindow);
  110.     
  111.     // Next we need to find a RAVE engine and then build a DrawContext to draw into it.  First, we'll find
  112.     // an engine that is capable of drawing to the selected GDevice.
  113.  
  114.     qaDevice.deviceType = kQADeviceGDevice;
  115.     qaDevice.device.gDevice = device;
  116.     
  117.     // We have to explicitly enable the Apple hardware card because it isn't 100% RAVE compliant.
  118.     // If we use this engine, we have to write special case code to deal with it.
  119. #if qEnableAppleHardware
  120.     QAEngineEnable (kQAVendor_Apple,kQAEngine_AppleHW);
  121. #endif
  122.     
  123.     // We'll just grab the first engine that the system offers us.
  124.     theEngine = FindTextureMappingEngine (&qaDevice);
  125.     FAIL_NIL (theEngine, "\pERROR:  No RAVE engines available that can texture map.")
  126.     
  127.     // Next we need to set up the a QARect with the Window's rectangle in GDevice local coordinates.
  128.     
  129.     qaBoundsRect.left = bounds.left - deviceRect.left;
  130.     qaBoundsRect.right = bounds.right  - deviceRect.left;
  131.     qaBoundsRect.top = bounds.top - deviceRect.top;
  132.     qaBoundsRect.bottom = bounds.bottom - deviceRect.top;
  133.  
  134.     // If we wanted to create a clipping region, we'd initialize a clip here.
  135.     
  136.     // Finally, we're ready to create the draw context!
  137.     theQAErr = QADrawContextNew (&qaDevice, &qaBoundsRect, NULL, theEngine, 
  138.                                  kQAContext_DeepZ | kQAContext_DoubleBuffer, &theContext);
  139.     if (theQAErr != kQANoErr) SIGNAL_ERROR ("\pERROR:  Failed to create RAVE draw context")
  140.     FAIL_NIL (theContext, "\pERROR:  Failed to create RAVE draw context")
  141.     
  142.     // set the background to black
  143.     QASetFloat (theContext, kQATag_ColorBG_a, 1.0);
  144.     QASetFloat (theContext, kQATag_ColorBG_r, 0.0);
  145.     QASetFloat (theContext, kQATag_ColorBG_g, 0.0);
  146.     QASetFloat (theContext, kQATag_ColorBG_b, 0.0);
  147.     
  148.     // Create the texture we'll use to animate our triangle.
  149.     texture = LoadTextureFromPictResource (theEngine, 128);
  150.     FAIL_NIL (texture, "\pERROR: Failed to load the texture from the resource")
  151.     
  152.     while (!Button())
  153.     {
  154.     theTriangle[0].x = 160 + 150*gCosArray[loop];
  155.     theTriangle[0].y = 160 + 150*gSinArray[loop];
  156.     theTriangle[0].z = 0.25;
  157.     theTriangle[0].invW = 1.0;
  158.     theTriangle[0].a = 1.0;
  159.     theTriangle[0].r = 0.0;
  160.     theTriangle[0].g = 0.0;
  161.     theTriangle[0].b = 0.0;
  162.     theTriangle[0].uOverW = 0.5;
  163.     theTriangle[0].vOverW = 0;
  164.     
  165.     theTriangle[1].x = 160 + 150*gCosArray[(loop+240) % 720];
  166.     theTriangle[1].y = 160 + 150*gSinArray[(loop+240) % 720];
  167.     theTriangle[1].z = 0.25;
  168.     theTriangle[1].invW = 1.0;
  169.     theTriangle[1].a = 1.0;
  170.     theTriangle[1].r = 0.0;
  171.     theTriangle[1].g = 0.0;
  172.     theTriangle[1].b = 0.0;
  173.     theTriangle[1].uOverW = 0;
  174.     theTriangle[1].vOverW = 1;
  175.     
  176.     theTriangle[2].x = 160 + 150*gCosArray[(loop+480) % 720];
  177.     theTriangle[2].y = 160 + 150*gSinArray[(loop+480) % 720];
  178.     theTriangle[2].z = 0.25;
  179.     theTriangle[2].invW = 1.0;
  180.     theTriangle[2].a = 1.0;
  181.     theTriangle[2].r = 0.0;
  182.     theTriangle[2].g = 0.0;
  183.     theTriangle[2].b = 0.0;
  184.     theTriangle[2].uOverW = 1;
  185.     theTriangle[2].vOverW = 1;
  186.  
  187.     QARenderStart (theContext, NULL, NULL);
  188.     QASetPtr (theContext, kQATag_Texture, texture);
  189.     QADrawTriTexture (theContext, &theTriangle[0], &theTriangle[1], &theTriangle[2], kQATriFlags_None);
  190.     QARenderEnd(theContext, NULL);
  191.     
  192.     // Increment our loop position    
  193.     loop = (loop+5) % 720;
  194.     }
  195.  
  196.  
  197.     error:
  198.     if (texture != NULL)
  199.         QATextureDelete (theEngine, texture);
  200.     if (theContext != NULL)
  201.         QADrawContextDelete (theContext);
  202.     if (theWindow != NULL)
  203.         DisposeWindow(theWindow);
  204.     return;
  205. }